#include "stdio.h"
#include<stdlib.h>
#include "SDL/SDL.h"
#include"VariableGlobale.h"
#include"Fonction.h"



int recomencer ()
{
    int i,j;

    for(i=0; i<5; i++)
    {
        for(j=0; j<4; j++)
        {

            temp = SDL_LoadBMP("img/rpn.bmp");
            if (temp == NULL)
            {
                printf("Unable to load bitmap: %s\n", SDL_GetError());
                return 1;
            }
            PionB[i][j] = SDL_DisplayFormat(temp);
            SDL_FreeSurface(temp);
            SDL_SetColorKey(PionB[i][j], SDL_SRCCOLORKEY, SDL_MapRGB(PionB[i][j]->format, 255, 255, 255));
            if (j%2==0)
            {
                posB[i][j].y = 66 + (i)*2*57;
            }
            else
            {
                posB[i][j].y = 66 + 57+(i*2*57);
            }
            posB[i][j].x = 206 + j*57;
            posB[i][j].w = PionB[i][j]->w;
            posB[i][j].h = PionB[i][j]->h;
        }
    }

    for(i=0; i<5; i++)
    {
        for(j=0; j<4; j++)
        {

            temp = SDL_LoadBMP("img/bpn.bmp");
            if (temp == NULL)
            {
                printf("Unable to load bitmap: %s\n", SDL_GetError());
                return 1;
            }
            PionN[i][j] = SDL_DisplayFormat(temp);
            SDL_FreeSurface(temp);
            SDL_SetColorKey(PionN[i][j], SDL_SRCCOLORKEY, SDL_MapRGB(PionN[i][j]->format, 255, 255, 255));
            if (j%2==0)
            {
                posN[i][j].y = 66 + (i)*2*57;
            }
            else
            {
                posN[i][j].y = 66 + 57+(i*2*57);
            }
            posN[i][j].x = 548 + j*57;
            posN[i][j].w = PionN[i][j]->w;
            posN[i][j].h = PionN[i][j]->h;
        }
    }
    for(i=0; i<20; i++)
    {
        posReat[i].y=2000;
        posBeat[i].y=2000;
    }
    for(i=0; i<20; i++)
    {
        posDR[i].x = 2000;
        posDN[i].x = 2000;
    }
    tour=3;
    j3=0;
    j2=0;
    op=0;
    posO.x = 8000;
    posWR.y=2000;
    posWN.y=2000;
    Win=0;
    posTB.x=2000;
    posTR.x=201;
    return 0;
}

int Regles()
{
    int DoneRegle=1;
    screen = SDL_SetVideoMode(767, 559, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    temp = SDL_LoadBMP("img/Regle.bmp");
    image = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    posO.x = 0;
    posO.y = 0;
    posO.w = image->w;
    posO.h = image->h;

    while(DoneRegle)
    {

        SDL_BlitSurface(image,NULL , screen, &posO);
        SDL_Flip(screen);
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                DoneRegle = 0;
                break;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    DoneRegle = 0;
                break;
            }
            }
        }

    }
    return 0;
}

int jouer ()
{

    int i,j,Donejouer=1;

    tour=3;
    j3=0;
    j2=0;
    Win=0;

    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    screen = SDL_SetVideoMode(600, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));




    temp = SDL_LoadBMP("img/image.bmp");
    if (temp == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    image = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);



    for(i=0; i<5; i++)
    {
        for(j=0; j<4; j++)
        {

            temp = SDL_LoadBMP("img/rpn.bmp");
            if (temp == NULL)
            {
                printf("Unable to load bitmap: %s\n", SDL_GetError());
                return 1;
            }
            PionB[i][j] = SDL_DisplayFormat(temp);
            SDL_FreeSurface(temp);
            SDL_SetColorKey(PionB[i][j], SDL_SRCCOLORKEY, SDL_MapRGB(PionB[i][j]->format, 255, 255, 255));
            if (j%2==0)
            {
                posB[i][j].y = 66 + (i)*2*57;
            }
            else
            {
                posB[i][j].y = 66 + 57+(i*2*57);
            }
            posB[i][j].x = 206 + j*57;
            posB[i][j].w = PionB[i][j]->w;
            posB[i][j].h = PionB[i][j]->h;
        }
    }

    for(i=0; i<5; i++)
    {
        for(j=0; j<4; j++)
        {
            temp = SDL_LoadBMP("img/bpn.bmp");
            if (temp == NULL)
            {
                printf("Unable to load bitmap: %s\n", SDL_GetError());
                return 1;
            }
            PionN[i][j] = SDL_DisplayFormat(temp);
            SDL_FreeSurface(temp);
            SDL_SetColorKey(PionN[i][j], SDL_SRCCOLORKEY, SDL_MapRGB(PionN[i][j]->format, 255, 255, 255));
            if (j%2==0)
            {
                posN[i][j].y = 66 + (i)*2*57;
            }
            else
            {
                posN[i][j].y = 66 + 57+(i*2*57);
            }
            posN[i][j].x = 548 + j*57;
            posN[i][j].w = PionN[i][j]->w;
            posN[i][j].h = PionN[i][j]->h;
        }
    }

    for(i=0; i<10; i++)
    {
        temp = SDL_LoadBMP("img/bpr.bmp");
        DameN[i] = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
        SDL_SetColorKey(DameN[i], SDL_SRCCOLORKEY, SDL_MapRGB(DameN[i]->format, 255, 255, 255));
        posDN[i].x = 2000;
        posDN[i].y = 60;
        posDN[i].w = DameN[i]->w;
        posDN[i].h = DameN[i]->h;
    }

    for(i=0; i<10; i++)
    {
        temp = SDL_LoadBMP("img/rpr.bmp");
        DameR[i] = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
        SDL_SetColorKey(DameR[i], SDL_SRCCOLORKEY, SDL_MapRGB(DameR[i]->format, 255, 255, 255));
        posDR[i].x = 2000;
        posDR[i].y = 60;
        posDR[i].w = DameR[i]->w;
        posDR[i].h = DameR[i]->h;
    }

    for(i=0; i<20; i++)
    {
        temp = SDL_LoadBMP("img/pionBeat.bmp");
        PionBeat[i] = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
        SDL_SetColorKey(PionBeat[i], SDL_SRCCOLORKEY, SDL_MapRGB(PionBeat[i]->format, 255, 255, 255));
        posBeat[i].x = 115;
        posBeat[i].y = 5000;
        posBeat[i].w = PionBeat[i]->w;
        posBeat[i].h = PionBeat[i]->h;
    }

    for(i=0; i<20; i++)
    {
        temp = SDL_LoadBMP("img/pionReat.bmp");
        PionReat[i] = SDL_DisplayFormat(temp);
        SDL_FreeSurface(temp);
        SDL_SetColorKey(PionReat[i], SDL_SRCCOLORKEY, SDL_MapRGB(PionReat[i]->format, 255, 255, 255));
        posReat[i].x = 817;
        posReat[i].y = 5000;
        posReat[i].w = PionReat[i]->w;
        posReat[i].h = PionReat[i]->h;
    }

    temp = SDL_LoadBMP("img/WinR.bmp");
    WinR = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(WinR, SDL_SRCCOLORKEY, SDL_MapRGB(WinR->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/WinN.bmp");
    WinN = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(WinN, SDL_SRCCOLORKEY, SDL_MapRGB(WinN->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/eat.bmp");
    EatB = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(EatB, SDL_SRCCOLORKEY, SDL_MapRGB(EatB->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/eat.bmp");
    EatN = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(EatN, SDL_SRCCOLORKEY, SDL_MapRGB(EatN->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/Red.bmp");
    Red = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(Red, SDL_SRCCOLORKEY, SDL_MapRGB(Red->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/Blk.bmp");
    Blk = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(Blk, SDL_SRCCOLORKEY, SDL_MapRGB(Blk->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/option.bmp");
    option = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    temp = SDL_LoadBMP("img/TurnR.bmp");
    TurnR = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(TurnR, SDL_SRCCOLORKEY, SDL_MapRGB(TurnR->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/TurnB.bmp");
    TurnB = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(TurnB, SDL_SRCCOLORKEY, SDL_MapRGB(TurnB->format, 255, 255, 255));

    posWR.x = 400;
    posWR.y = 2000;
    posWR.w = WinR->w;
    posWR.h = WinR->h;

    posWN.x = 400;
    posWN.y = 2000;
    posWN.w = WinN->w;
    posWN.h = WinN->h;

    posTB.x = 2200;
    posTB.y = 21;
    posTB.w = TurnB->w;
    posTB.h = TurnB->h;

    posTR.x = 201;
    posTR.y = 20;
    posTR.w = TurnR->w;
    posTR.h = TurnR->h;

    posBlk.x = 800;
    posBlk.y = 20;
    posBlk.w = Blk->w;
    posBlk.h = Blk->h;

    posRED.x = 100;
    posRED.y = 20;
    posRED.w = Red->w;
    posRED.h = Red->h;

    posEB.x = 800;
    posEB.y = 66;
    posEB.w = EatB->w;
    posEB.h = EatB->h;

    posEN.x = 100;
    posEN.y = 66;
    posEN.w = EatN->w;
    posEN.h = EatN->h;

    posO.x = 8000;
    posO.y = 56;
    posO.w = option->w;
    posO.h = option->h;

    dest.x = 200;
    dest.y = 60;
    dest.w = image->w;
    dest.h = image->h;

    while(Donejouer)
    {
        screen = SDL_SetVideoMode(980, 640, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
        if (screen == NULL)
        {
            printf("Unable to set video mode: %s\n", SDL_GetError());
            return 1;
        }
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));
        SDL_BlitSurface(EatB, NULL, screen, &posEB);
        SDL_BlitSurface(EatN, NULL, screen, &posEN);
        SDL_BlitSurface(Red, NULL, screen, &posRED);
        SDL_BlitSurface(Blk, NULL, screen, &posBlk);
        SDL_BlitSurface(TurnR, NULL, screen, &posTR);
        SDL_BlitSurface(TurnB, NULL, screen, &posTB);
        SDL_BlitSurface(image, NULL, screen, &dest);
        for(i=0; i<5; i++)
        {
            for(j=0; j<4; j++)
            {
                SDL_BlitSurface(PionB[i][j], NULL, screen, &posB[i][j]);
                SDL_BlitSurface(PionN[i][j], NULL, screen, &posN[i][j]);
            }
        }
        for(i=0; i<10; i++)
        {
            SDL_BlitSurface(DameN[i], NULL, screen, &posDN[i]);
            SDL_BlitSurface(DameR[i], NULL, screen, &posDR[i]);
        }

        for(i=0; i<20; i++)
        {
            SDL_BlitSurface(PionBeat[i], NULL, screen, &posBeat[i]);
            SDL_BlitSurface(PionReat[i], NULL, screen, &posReat[i]);
        }

        SDL_GetMouseState(&xm, &ym);
        im=(xm-200)/57;
        jm=(ym-60)/57;
        SDL_BlitSurface(option, NULL, screen, &posO);
        SDL_BlitSurface(WinR, NULL, screen, &posWR);
        SDL_BlitSurface(WinN, NULL, screen, &posWN);
        SDL_Flip(screen);

        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                Donejouer = 0;
                break;
            case SDL_KEYDOWN:
            {

                if (event.key.keysym.sym == SDLK_ESCAPE)
                    Donejouer = 0;
                break;
            }
            case SDL_MOUSEMOTION :
                if(( event.motion.x >=200)&&(event.motion.x<=595)&&(event.motion.y >=26)&&(event.motion.y<=58))
                {
                    posO.x = 200;
                    op=1;
                }
                else
                {
                    if (( event.motion.x >=200)&&(event.motion.x<=595)&&(event.motion.y >=26)&&(event.motion.y<=225)&&(op==1))
                    {
                        posO.x = 200;
                    }
                    else
                    {
                        op=0;
                        posO.x = 8000;
                    }

                }
                break;
            case SDL_MOUSEBUTTONDOWN:
            {

                if(Win==0)
                {
                    if((tour%2==1))
                    {
                        if((event.motion.x <=800)&&(event.motion.x >=200)&&(event.motion.y>=60)&&(event.motion.y<=630)&&(op==0)&&(j2==0)&&(event.button.button == SDL_BUTTON_LEFT))
                        {
                            for(i=0; i<20; i++)
                            {
                                if ((posDR[i].x==im*57+206)&&(posDR[i].y==jm*57+66))
                                {
                                    p=i;
                                    j2=1;
                                    i2=1;
                                    iold=im;
                                    jold=jm;
                                    etat2=2;
                                }
                            }
                            for(i=0; i<5; i++)
                            {
                                for(j=0; j<4; j++)
                                {
                                    if ((posB[i][j].x==im*57+206)&&(posB[i][j].y==jm*57+66))
                                    {
                                        p=i;
                                        k=j;
                                        j2=1;
                                        i2=1;
                                        iold=im;
                                        jold=jm;
                                        etat2=1;
                                    }
                                }
                            }
                        }
                        if ((verifie_place(im,jm)==0)&&(event.motion.x <=800)&&(event.motion.x >=200)&&(event.motion.y>=60)&&(event.motion.y<=630)&&(op==0)&&(i2==0)&&(j2==1)&&(event.button.button == SDL_BUTTON_LEFT))
                        {
                            if (verifie_move_Red()==1) move_Red();
                            if (verifie_move_Red()==2) prise_Red();
                            if (verifie_move_Red()==11) move_Dame_Red();
                            if (verifie_move_Red()==22) prise_Dame_Red();
                        }

                        if((op==0)&&(j2==1))
                        {
                            i2=0;
                        }
                    }
                    if((tour%2==0))
                    {
                        if((event.motion.x <=800)&&(event.motion.x >=200)&&(event.motion.y>=60)&&(event.motion.y<=630)&&(op==0)&&(j3==0)&&(event.button.button == SDL_BUTTON_LEFT))
                        {
                            for(i=0; i<20; i++)
                            {
                                if ((posDN[i].x==im*57+206)&&(posDN[i].y==jm*57+66))
                                {
                                    p=i;
                                    j3=1;
                                    i3=1;
                                    iold=im;
                                    jold=jm;
                                    etat3=2;
                                }
                            }
                            for(i=0; i<5; i++)
                            {
                                for(j=0; j<4; j++)
                                {
                                    if ((posN[i][j].x==im*57+206)&&(posN[i][j].y==jm*57+66))
                                    {
                                        p=i;
                                        k=j;
                                        j3=1;
                                        i3=1;
                                        iold=im;
                                        jold=jm;
                                        etat3=1;
                                    }
                                }
                            }
                        }
                        if ( (verifie_place(im,jm)==0)&&(event.motion.x <=800)&&(event.motion.x >=200)&&(event.motion.y>=60)&&(event.motion.y<=630)&&(op==0)&&(i3==0)&&(j3==1)&&(event.button.button == SDL_BUTTON_LEFT))
                        {
                            if(verifie_move_Black()==1)
                            {
                                move_Black();
                            }
                            if(verifie_move_Black()==2)
                            {
                                prise_Black();
                            }
                            if(verifie_move_Black()==11)
                            {
                                move_Dame_Black();
                            }
                            if(verifie_move_Black()==22)
                            {
                                prise_Dame_Black();
                            }
                        }

                        if((op==0)&&(j3==1))
                        {
                            i3=0;
                        }
                    }
                    if ((Win==1)&&( event.motion.x >=400)&&(event.motion.x<=600)&&(event.motion.y >=200)&&(event.motion.y<=400))
                    {
                        recomencer();
                    }
                    if (( event.motion.x >=200)&&(event.motion.x<=400)&&(event.motion.y >=160)&&(event.motion.y<=210)&&(op==1))
                    {
                        Donejouer=0;
                    }
                    if (( event.motion.x >=200)&&(event.motion.x<=400)&&(event.motion.y >=50)&&(event.motion.y<=110)&&(op==1))
                    {
                        recomencer();
                    }
                    if (( event.motion.x >=200)&&(event.motion.x<=400)&&(event.motion.y >=110)&&(event.motion.y<=160)&&(op==1))
                    {
                        Regles();
                    }
                    if(event.button.button == SDL_BUTTON_RIGHT)
                    {
                        if(j2==1)
                        {
                            if (etat2==1)
                            {
                                posB[p][k].x=iold*57+206;
                                posB[p][k].y=jold*57+66;
                                j2=0;
                                i2=1;
                            }
                            if (etat2==2)
                            {
                                posDR[p].x=iold*57+206;
                                posDR[p].y=jold*57+66;
                                j2=0;
                                i2=1;
                            }
                        }
                        if(j3==1)
                        {
                            if (etat3==1)
                            {
                                posN[p][k].x=iold*57+206;
                                posN[p][k].y=jold*57+66;
                                j3=0;
                                i3=1;
                            }
                            if (etat3==2)
                            {
                                posDN[p].x=iold*57+206;
                                posDN[p].y=jold*57+66;
                                j3=0;
                                i3=1;
                            }
                        }
                    }
                    break;
                }
            }
            }
            if((op==0)&&(j3==1)&&(etat3==2))
            {
                posDN[p].x=xm;
                posDN[p].y=ym;
            }
            if((op==0)&&(j2==1)&&(etat2==2))
            {
                posDR[p].x=xm;
                posDR[p].y=ym;
            }
            if((op==0)&&(j2==1)&&(etat2==1))
            {
                posB[p][k].x=xm;
                posB[p][k].y=ym;
            }
            if((op==0)&&(j3==1)&&(etat3==1))
            {
                posN[p][k].x=xm;
                posN[p][k].y=ym;
            }

        }
    }
    return 0;
}

int main(int argc, char *argv[])
{
    int i,j,Done=1;
    SDL_Surface *image1;
    SDL_Surface *Fond;

    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    screen = SDL_SetVideoMode(551, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/Menu.bmp");
    Fond = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(Fond, SDL_SRCCOLORKEY, SDL_MapRGB(Fond->format, 255, 255, 255));

    temp = SDL_LoadBMP("img/Selct.bmp");
    image1 = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    SDL_SetColorKey(image1, SDL_SRCCOLORKEY, SDL_MapRGB(image1->format, 255, 255, 255));

    PosF.x = 0;
    PosF.y = 0;
    PosF.w = Fond->w;
    PosF.h = Fond->h;

    posS.x = 0;
    posS.y = 50;
    posS.w = image1->w;
    posS.h = image1->h;

    while(Done)
    {

        screen = SDL_SetVideoMode(563, 510
                                  , 32, SDL_HWSURFACE| SDL_DOUBLEBUF);
        if (screen == NULL)
        {
            printf("Unable to set video mode: %s\n", SDL_GetError());
            return 1;
        }
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));
        SDL_BlitSurface(Fond, NULL, screen, &PosF);
        SDL_BlitSurface(image1,NULL , screen, &posS);
        SDL_Flip(screen);



        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                Done = 0;
                break;
            case SDL_KEYDOWN:
            {

                if (event.key.keysym.sym == SDLK_ESCAPE)
                    Done = 0;
                break;
            }
            case SDL_MOUSEMOTION :
                if( (event.motion.x >=80) && (event.motion.x <=400) && (event.motion.y <=120)&& (event.motion.y >=0)) posS.y = 50;
                if( (event.motion.x >=80) && (event.motion.x <=500) && (event.motion.y <=240)&& (event.motion.y >=120)) posS.y = 170;
                if( (event.motion.x >=80) && (event.motion.x <=500) && (event.motion.y <=360)&& (event.motion.y >=240)) posS.y = 290;
                if( (event.motion.x >=80) && (event.motion.x <=400) && (event.motion.y <=480)&& (event.motion.y >=360)) posS.y = 410;
                break;
            case SDL_MOUSEBUTTONUP:
                if( (event.motion.x >=80) && (event.motion.x <=400) && (event.motion.y <=120)&& (event.motion.y >=0))
                {
                    jouer();
                }
                if( (event.motion.x >=80) && (event.motion.x <=500) && (event.motion.y <=240)&& (event.motion.y >=190))
                {
                    Regles();
                }
                if( (event.motion.x >=80) && (event.motion.x <=500) && (event.motion.y <=360)&& (event.motion.y >=240)) {}
                if( (event.motion.x >=80) && (event.motion.x <=400) && (event.motion.y <=480)&& (event.motion.y >=360))Done=0;
                break;
            }
        }
    }
    return 0;
}
